import 'package:whale/helper/utils/util.dart';
import 'package:whale/pages/im/util/im_global.dart';
import 'package:whale/pages/im/model/msg.dart';
import 'package:whale/pages/im/model/user.dart';

enum IMMsgStatus {
  sending,
  success,
  fail,
}

class ImModel {
  String id;

  Map<String, dynamic> fromJson;
  Map<String, dynamic> msgJson;

  late ImUserModel fromUser;

  ImUserModel get me {
    return ImUserModel(
      id: IMGlobal.instanse.user.id,
      name: '企明星',
      avater:
          'https://img0.baidu.com/it/u=1994310215,3345087627&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
    );
  }

  late bool isSelf;

  late ImMsgType msgType;

  late ImMsgModel msg;

  IMMsgStatus get msgStatus {
    return msg.status;
  }

  ImModel({
    required this.id,
    required this.fromJson,
    required this.msgJson,
  }) {
    _setupData();
  }

  void _setupData() {
    msg = ImMsgModel.fromJson(msgJson);
    msgType = msg.type;
    isSelf = msg.userId == IMGlobal.instanse.user.id;
    fromUser = ImUserModel.fromJson(fromJson);
  }

  static ImModel createSelfMsgInfo(ImMsgModel msg, {required ImMsgType type}) {
    var m = ImModel(id: '1', fromJson: {}, msgJson: {});

    var data = msg.toJson();
    data.addAll({
      'time': DateFormat.format(DateTime.now(), DateFormatType.hm),
      'type': msg.getTypeValue(type),
      'userId': m.me.id,
    });

    m.fromJson = m.me.toJson();
    m.msgJson = data;
    m._setupData();
    return m;
  }

  factory ImModel.createText(String text) {
    var msg = ImMsgModel.fromJson({'content': text});
    return createSelfMsgInfo(msg, type: ImMsgType.text);
  }

  factory ImModel.createVoice(String url, {required int seconds}) {
    var msg = ImMsgVoiceModel(json: {'content': url, 'seconds': seconds});
    return createSelfMsgInfo(msg, type: ImMsgType.voice);
  }

  factory ImModel.createPicture(
    String url, {
    double? width,
    double? height,
  }) {
    var msg = ImMsgImageModel(json: {
      'content': url,
      'thumb': url,
      'w': width ?? 300,
      'h': height ?? 400
    });
    return createSelfMsgInfo(msg, type: ImMsgType.image);
  }
  factory ImModel.createVideo(String url, {required int seconds}) {
    var msg = IMMsgVideoModel(json: {'content': url, 'seconds': seconds});
    return createSelfMsgInfo(msg, type: ImMsgType.video);
  }
  factory ImModel.createMap(
    String url, {
    required double lat,
    required double lng,
    required String title,
  }) {
    var msg = ImMsgMapModel(
        json: {'content': url, 'lat': lat, 'lng': lng, 'title': title});
    return createSelfMsgInfo(msg, type: ImMsgType.map);
  }
  factory ImModel.createFile(
    String url, {
    required int size,
    required String title,
    required String format,
  }) {
    var msg = IMMsgFileModel(
        json: {'content': url, 'size': size, 'format': format, 'title': title});
    return createSelfMsgInfo(msg, type: ImMsgType.file);
  }

  factory ImModel.createCard(
    String url, {
    required String title,
    required String des,
    required String image,
    required String sourceIcon,
    required String sourceName,
  }) {
    var msg = IMMsgCardModel(json: {
      'content': url,
      'des': des,
      'image': image,
      'title': title,
      'sourceIcon': sourceIcon,
      'sourceName': sourceName,
    });
    return createSelfMsgInfo(msg, type: ImMsgType.card);
  }
}
